package com.atung.parking.controller;
import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.entity.BusSuperUser;
import com.atung.parking.enums.AdminType;
import com.atung.parking.export.PublicExportUtil;
import com.atung.parking.mapstruct.SuperUserConverter;
import com.atung.parking.request.query.QuerySuperUserRequest;
import com.atung.parking.request.save.SaveOrUpdateSuperUserRequest;
import com.atung.parking.request.save.UpdateSuperUserStateRequest;
import com.atung.parking.response.page.ApprovalRecordResponse;
import com.atung.parking.response.page.SimpleSuperUserResponse;
import com.atung.parking.response.page.SuperUserResponse;
import com.atung.parking.service.SuperUserManegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/super")
@Api(tags = "客户管理")
public class SuperUserManegeController {

    @Resource
    private SuperUserManegeService superUserManegeService;
    @Resource
    private SuperUserConverter superUserConverter;


    @PostMapping("/save")
    @ApiOperation("新增客戶")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult saveSuperUser(@RequestBody @Valid SaveOrUpdateSuperUserRequest request, HttpServletRequest httpServletRequest) {
        return superUserManegeService.saveSuperUserService(request, httpServletRequest);
    }

    @PostMapping("/update/state")
    @ApiOperation("修改客户状态")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult updateSuperUserState(@RequestBody @Valid UpdateSuperUserStateRequest request) {
        return superUserManegeService.updateSuperUserStateService(request);
    }


    @GetMapping("/userName")
    @ApiOperation("获取客户下拉列表数据")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult<List<SimpleSuperUserResponse>> querySuperUserNameList() {
        List<SimpleSuperUserResponse> simpleSuperUserResponseList = superUserManegeService.querySuperUserNameListService();
        return ApiResult.ok(simpleSuperUserResponseList);
    }

    @GetMapping("/query")
    @ApiOperation("获取客户列表")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult<PageResponse<SuperUserResponse>> querySuperUserList(QuerySuperUserRequest request) {
        PageInfoResponse<BusSuperUser> superUserPageDto = superUserManegeService.querySuperUserListService(request);
        List<SuperUserResponse> collect = superUserPageDto.getData().stream().map(superUserConverter::toResponse).collect(Collectors.toList());
        return ApiResult.ok(collect, request.getPageNum(), request.getPageSize(), superUserPageDto.getTotal());
    }
    @GetMapping("/export")
    @ApiOperation("导出客户列表")
//    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public void  exportSuperUserList(QuerySuperUserRequest request, HttpServletResponse httpServletResponse) {
        superUserManegeService.exportSuperUserListService(request,httpServletResponse);
    }
    @GetMapping("/valid/phone/{phone}")
    @ApiOperation("校验手机号是否存在")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult validPhone(@PathVariable("phone") String phone) {
        BusSuperUser busSuperUser = superUserManegeService.queryBusSuperByPhoneService(phone);
        if (Objects.isNull(busSuperUser)) {
            return ApiResult.ok(true);
        }
        return ApiResult.ok(false);
    }
    @GetMapping("/query/approval/{id}")
    @ApiOperation("获取客户操作记录")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult<List<ApprovalRecordResponse>> queryApprovalParkingIotPackageList(@PathVariable("id") Long id) {
        return ApiResult.ok(superUserManegeService.queryApprovalSuperAdminListService(id));
    }

}
